﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlightSearchServer
{
    class RegistrationService : IRegistrationService
    {
        public void register(string endpoint, string serverName, string alliance, ServersCollection replications)
        {
            SearchServer server = new SearchServer();
            server.Name = serverName;
            server.Alliance = alliance;
            server.Endpoint = endpoint;
            foreach (SearchServer srv in replications)
            {
                ServersManager.Instance.replicateAt( srv, server);
            }
            ServersManager.Instance.addNewServer( server);
            // Failed server might be recovered since all previous data need to invalidate 
            // from the cache entry.
            GlobalResultsCache.Instance.invalidateCache();
            LocalClientCache.Instance.invalidateEntry( serverName + "&" + alliance);
            foreach( var t in replications)
                LocalClientCache.Instance.invalidateEntry(t.Name + "&" + t.Alliance);
        }

        public void update(string serverName, string alliance, ServersCollection replications)
        {
            ServersManager.Instance.updateServer( serverName, alliance, replications);
            foreach (var t in replications)
                LocalClientCache.Instance.invalidateEntry(t.Name + "&" + t.Alliance);
        }
    }
}
